#include "Defect_Side.h"

Defect_Side::Defect_Side()
{

}
int Defect_Side::D_Side(HObject Img,HObject &OutImg)
{
    // Local iconic variables
    HObject  EmptyObject, Region,ImageR,Rect;
    HObject  ConnectedRegions, SelectedRegions, RegionTrans;
    HObject  Rectangle, ImageReduced, Region1, ConnectedRegions1;
    HObject  SelectedRegions1, RegionUnion1, ConnectedRegions2;
    HObject  SelectedRegions2, RegionTrans2, RegionErosion;
    HObject  RegionDifference, ImageResult1, ImageResult2;
    HObject  ImageResult3, MultiChannelImage;

    // Local control variables
    HTuple  Row, Column;
    HTuple  Phi, Length1, Length2, step, Index1;
    HTuple  Mean, Deviation,Num;
    HTuple  Area, Row1, Column1;
    int flag = 0;

    HTuple  parm;
    //参数 1  产品定位阈值 128
    parm[0] = m_part.para1;
    //**参数2 内缩长度  20
    parm[1] = m_part.para2;
    //**参数3 内缩高度 5
    parm[2] = m_part.para3;
    //**参数4 缺陷定位阈值 10
    parm[3] = m_part.para4;
    //**参数5 定位产品面积 2000
    parm[4] = m_part.para5;
    //**参数6 缺陷面积 50
    parm[5] = m_part.para6;
    //**参数7 是否带导角 1
    parm[6] = m_part.para7;
    try
    {
        Num=0;
        OutImg=Img;
        GenEmptyObj(&EmptyObject);
        GenRectangle1(&Rect, m_part2.x1, m_part2.y1, m_part2.x2, m_part2.y2);
        ReduceDomain(Img, Rect, &ImageR);
        Threshold(ImageR, &Region, parm[0], 255);
        Connection(Region, &ConnectedRegions);
        SelectShapeStd(ConnectedRegions, &SelectedRegions, "max_area", 70);
        AreaCenter (SelectedRegions, &Area, &Row1, &Column1);
		if ((Area.D()<parm[5]))
        {
            return 0;
        }

        if(parm[6]==1)
        {
            ShapeTrans(SelectedRegions, &SelectedRegions, "convex");
        }
        else
        {
            ShapeTrans(SelectedRegions, &SelectedRegions, "rectangle1");
        }

        ErosionRectangle1(SelectedRegions, &SelectedRegions, parm[1], parm[2]);
        ShapeTrans(SelectedRegions, &RegionTrans, "inner_rectangle1");
        SmallestRectangle2(RegionTrans, &Row, &Column, &Phi, &Length1, &Length2);
        step = (2*Length1)/5;
		
        TupleRound(step, &step);
		int a = step.D();
        {
            HTuple end_val18 = step;
            HTuple step_val18 = 1;
            for (Index1=1; Index1.Continue(end_val18, step_val18); Index1 += step_val18)
            {


                GenRectangle2(&Rectangle, (Row-Length1)+(5*Index1), Column,
                              Phi, 5, Length2);
                ReduceDomain(Img, Rectangle, &ImageReduced);
                Intensity(Rectangle, ImageReduced, &Mean, &Deviation);
				if (Mean.D() > 150)
				{
					return 0;
				}
                Threshold(ImageReduced, &Region1, 0, (Mean-Deviation)-parm[3]);
                Union2(EmptyObject, Region1, &EmptyObject);
            }
        }

        Connection(EmptyObject, &ConnectedRegions1);
        SelectShape(ConnectedRegions1, &SelectedRegions1, "area", "and", parm[5], 99999);
       /* DilationRectangle1(SelectedRegions1, &SelectedRegions1, 2, 2);
        Union1(SelectedRegions1, &RegionUnion1);
        Connection(RegionUnion1, &ConnectedRegions2);
		SelectShape(ConnectedRegions2, &SelectedRegions2, "area", "and", 100, 99999);*/

		CountObj(SelectedRegions1, &Num);
        if(Num.D()>0)
        {
			ShapeTrans(SelectedRegions1, &RegionTrans2, "outer_circle");
            DilationCircle(RegionTrans2, &RegionErosion, 3.5);
            Difference(RegionErosion, RegionTrans2, &RegionDifference);
            PaintRegion(RegionDifference, Img, &ImageResult1, 255, "fill");
            PaintRegion(RegionDifference, Img, &ImageResult2, 0, "fill");
            PaintRegion(RegionDifference, Img, &ImageResult3, 0, "fill");
            Compose3(ImageResult1, ImageResult2, ImageResult3, &MultiChannelImage);
            OutImg=MultiChannelImage;
            flag = 1;
        }
        return flag;

    }
    catch(HException ex)
    {
        return 0;
    }




}
